<?php
include("../common.inc.php");
InitGP(array("page","action","gid","search")); //初始化变量全局返回
include("function_common.php");
//初始化团购对象
include(INC_PATH."/group.class.php");
$gp=new GroupClass();

if($action=="" || empty($action)){
	
	InitGP(array("gid")); //初始化變量全局返回
	
	if(!empty($gid))$wherestr[]="gid in ('{$gid}')";
	$wherestr[] = "state=1" ;
	if(!empty($wherestr)) $wheresql = implode(' AND ', $wherestr);	//條件匯總
	
	//获取当前页码
	$total=$gp->getcount($wheresql); 							  //总信息数
	$pagesize=15;												  //一页显示信息数
	$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
	$offset=($page-1)*$pagesize;   								  //偏移量
	$dataarray=$gp->getdata("$offset,$pagesize","ALL",$wheresql); //获取团购数据
	
	foreach($dataarray as $k=>$v){
		if($v['group_type'] == 1){
			$dataarray[$k]['group_type'] = "短期團購" ;
		}elseif($v['group_type'] == 2){
			$dataarray[$k]['group_type'] = "長期團購" ;
		}elseif($v['group_type'] == 3){
			$dataarray[$k]['group_type'] = "活動" ;
		}
	}
	
	//包含后台模板文件
	include("tpl/groups_list.htm");
	
}elseif ($action=="recycle") {
	//获取当前页码
	$total=$gp->getcount("state=0"); //总信息数
	$pagesize=15;			//一页显示信息数
	$page = isset($page) ? max(1, intval($page)) : 1;//处理页码变量
	$offset=($page-1)*$pagesize;   //偏移量
	$dataarray=$gp->getdata("$offset,$pagesize","ALL","state=0"); //获取团购数据
	//包含后台模板文件
	include("tpl/groups_list.htm");	

}elseif ($action=="restore") {
	if(GetNum($gid)){
		editstate("groups","state","gid='{$gid}'",1);
		showmsg("恢复成功!","groups_list.php");//成功
	}
}elseif ($action=="refund") {
	//团购失败时候退款给用户帐户
	if(GetNum($gid)){
		include_once(ROOT_PATH."/includes/member.class.php");
		$m=new memberclass();
		include(INC_PATH."/order.class.php");
		$o=new OrderClass();
		include_once(INC_PATH."/moneychanges.class.php");
		$mc=new MchangesClass();
		$orderarray=$o->getdata("","gid='{$gid}' and money_state in(1,2) and state<>3");
		foreach ($orderarray as $order){
			if ($order['order_incharge']>0) {
				$note="團購ID{$order['gid']}失敗,訂單:{$order['order_sn']}退款";//文字说明
				$m->moneyedit($order['uname'],$order['order_incharge'],$note);//修改用户账户
				//退款交易金额
				editstate("order","state","oid={$order['oid']}",3);//修改订单状态为取消 1完成 2作废 3退款给用户
				$mc->add($order['order_sn'],$order['order_total_price'],$order['order_incharge'],"團購失敗退款",0,2);//记录退款单
			}
		}
		showmsg("退款給用戶帳戶完成!","groups_list.php");//成功
	}
	
	
	
	
}elseif ($action=="autobind") {
	if(GetNum($gid)){
		if (getbondnum($gid,"all")<=0 || getbondnum($gid,"last")<=0) {
			showmsg("沒有剩餘團購卷可分配!","-1");//成功
		}
		include(INC_PATH."/order.class.php");
		$o=new OrderClass();
		include_once(INC_PATH."/groupbond.class.php");
		$bond=new GroupBondClass();
		$orderarray=$o->getdata("","gid='{$gid}' and money_state=2");
		foreach ($orderarray as $order){
			$num=0;
			$bondcount=$bond->getcount("gid='{$gid}' and order_sn='{$order['order_sn']}'");
			if ($bondcount>0) {
				$num=$order['num']-$bondcount;
			}else {
				$num=$order['num'];
			}
			if ($num>0) {
				$bond->binduser($order['uid'],$order['uname'],$order['order_sn'],$order['gid'],$num);
			}
		}
		showmsg("自動分配團購券完成!","groups_list.php");//成功
	}

}elseif ($action=="edit") {
	InitGP(array("gid","cid","group_type","Submit","sid","title","subtitle","sn","cityid","gtypeid","freefreight","order","weight","score","market_price","shop_price","earnest_money","stock","max_buynum","group_usernum","group_startnum","group_bond_end_time","group_begin_time","group_end_time","gallery_id","goods_gallerys","goods_reviews","goods_reviews_contents","goods_reviews_site","goods_brief","goods_about","seokeyword","seocontent","goods_reviews_content","goods_reviews_user","goods_reviews_url","goods_reviews_web")); //初始化变量全局返回
	if(isset($Submit) && isset($gid)){
		$maxnum=max(max(count($goods_reviews_url),count($goods_reviews_content)),max(count($goods_reviews_web),count($goods_reviews_user)));
		$contents=array();
		$contents=array();
		for ($i=0;$i<$maxnum;$i++){
			$contents[$i]['web']=$goods_reviews_web[$i];
			$contents[$i]['user']=$goods_reviews_user[$i];
			$contents[$i]['url']=$goods_reviews_url[$i];
			$contents[$i]['content']=$goods_reviews_content[$i];
		}
		$goods_reviews_contents=serialize($contents);
		if(is_array($goods_gallerys))$good_pics=getdotstring($goods_gallerys,'int');//格式化成id,id形式
		//处理数据
		$arrayedit=array(
			"sid"=> GetNum($sid),
			"cityid"=> GetNum($cid),
			"group_type"=> GetNum($group_type),
			"sn"=> Char_cv($sn),
			"title"=> Char_cv($title) ,
			"subtitle"=> Char_cv($subtitle) ,
			"gtypeid"=> GetNum($gtypeid),
			"freefreight"=>GetNum($freefreight) ,
			"order"=> GetNum($order),
			"weight"=> GetNum($weight),
			"shop_price"=> GetNum($shop_price),
			"market_price"=> GetNum($market_price),
			"earnest_money"=> GetNum($earnest_money),
			"score"=> GetNum($score),
			"stock"=> GetNum($stock),
			"max_buynum"=> GetNum($max_buynum),
			"goods_pic"=> GetNum($gallery_id),
			"goods_pics"=> $good_pics,
			"group_usernum"=> GetNum($group_usernum),
			"group_startnum"=> GetNum($group_startnum),
			"group_bond_end_time"=> strtotime($group_bond_end_time),
			"group_begin_time"=> strtotime($group_begin_time),
			"group_end_time"=>  strtotime($group_end_time),
			"goods_reviews"=>  HtmlReplace($goods_reviews,-1),
			"goods_reviews_contents"=>  HtmlReplace($goods_reviews_contents,-1),
			"goods_reviews_site"=>  HtmlReplace($goods_reviews_site,-1),
			"goods_brief"=>  HtmlReplace($goods_brief,-1),
//			"goods_about"=>  HtmlReplace($goods_about,-1),
			"goods_about"=>  $goods_about,
			"seokeyword"=>  HtmlReplace($seokeyword,-1),
			"seocontent"=>  HtmlReplace($seocontent,-1)
		);
		$info=$gp->edit($gid,$arrayedit);
		if($info=="OK"){
			showmsg("編輯團購成功!","groups_list.php");//成功
		}else showmsg($info,"-1");//出错！
	
	}else {
		$dataedit=$gp->getdata(1,"","gid={$gid}");
		$value=$dataedit[0];//获取第一条记录
		include(INC_PATH."/suppliers.class.php");
		$sp=new SupplierClass();
		$sidselect=$sp->getselect("sid",$value['sid']);//获取select
		include_once(INC_PATH."/city.class.php");
		$city=new CityClass();
		$cityselect=$city->getselect("cid",$value['cityid']);		
	
		//團購類型
		$group_type_select = getselectstr("group_type",array(1=>"短期團購",2=>"長期團購",3=>"活動"),$value['group_type']);
		
		include_once(INC_PATH.'/attach.class.php');
		$att=new AttachClass();
		
		if(!empty($value['goods_pics'])){
		$imgarray=$att->getdata("","aid in({$value['goods_pics']})");
		}
		if(is_array($imgarray)){
			foreach ($imgarray as $valueimg){
				$echoimg[$valueimg[aid]]=$valueimg;
			}
		}else $echoimg=array();
		
		//编辑团购信息
		//print_r($value);
		include("tpl/groups_edit.htm");	
	}
}elseif ($action=="del" && !empty($gid)){
	//执行删除操作放入回收站
	$gid=GetNum($gid);
	if($gp->del($gid,"r"))showmsg("删除团购成功！","groups_list.php");
	else showmsg("删除失败！","groups_list.php");
}elseif ($action=="delr" && !empty($gid)){
	//执行删除操作放入回收站
	$gid=GetNum($gid);
	if($gp->del($gid))showmsg("删除团购成功！","groups_list.php");
	else showmsg("删除失败！","-1");
	
}else {
	showmsg("没传递参数！","-1");
}

/**
 * 获取团购卷数量
 *
 * @param unknown_type $gid
 * @param all所有 last 剩余 used 已使用 $type
 * @return 数字
 */
function getbondnum($gid,$type="all"){
	include_once(INC_PATH."/groupbond.class.php");
	$gb=new GroupBondClass();
	if ($type=="all") {
		$wherestr="gid='{$gid}'";
	}elseif ($type=="last"){
		$wherestr="gid='{$gid}' and state=0";
	}elseif ($type=="used"){
		$wherestr="gid='{$gid}' and state=1";
	}
	$count=$gb->getcount($wherestr);
	return $count;	
}



?>

